package com.jn.okhttp.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jn.okhttp.R;
import com.jn.okhttp.entity.TrackingNum;

import java.util.List;

public class TrackingAdapter extends RecyclerView.Adapter<TrackingAdapter.ViewHolder> {
    private List<TrackingNum.DataDTO.TrackingDetailsDTO> mList;

    public TrackingAdapter(List<TrackingNum.DataDTO.TrackingDetailsDTO> list) {
        mList = list;
    }

    public void updateData(List<TrackingNum.DataDTO.TrackingDetailsDTO> list) {
        mList.clear();
        mList = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.tracking_item, parent, false);
        return new TrackingAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        TrackingNum.DataDTO.TrackingDetailsDTO trackingItem = mList.get(position);
        holder.textView1.setText(trackingItem.getTime());
        holder.textView2.setText(trackingItem.getDescription());
        holder.textView3.setText(trackingItem.getLocation());
        holder.textView4.setText(trackingItem.getShipmentStatus());
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView textView1;
        TextView textView2;
        TextView textView3;
        TextView textView4;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            textView1 = itemView.findViewById(R.id.tracking_time);
            textView2 = itemView.findViewById(R.id.tracking_desc);
            textView3 = itemView.findViewById(R.id.tracking_local);
            textView4 = itemView.findViewById(R.id.tracking_status);
        }
    }
}